<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Template {

    var $ci;

    function __construct() {
        $this->ci = & get_instance();
        $this->ci->load->library('Mobiledetect');
        $this->ci->load->library('session');
    }

    function load($tpl_view, $body_view = null, $data = null) {
        $mobile = $this->ci->session->userdata('mobile');
         if ($this->ci->mobiledetect->isMobile() && $mobile != 'off') { // mobile
//        if (!$this->ci->mobiledetect->isMobile()) { /// web
            // Any mobile device.
            if (!is_null($body_view)) {
                if (file_exists(APPPATH . 'views/mobiles/' . $tpl_view . '/' . $body_view)) {
                    $body_view_path = $tpl_view . '/' . $body_view;
                } else if (file_exists(APPPATH . 'views/mobiles/' . $tpl_view . '/' . $body_view . '.php')) {
                    $body_view_path = '/mobiles/' . $tpl_view . $body_view . '.php';
                } else if (file_exists(APPPATH . 'views/mobiles/' . $body_view)) {
                    $body_view_path = 'mobiles/' . $body_view;
                } else if (file_exists(APPPATH . 'views/mobiles/' . $body_view . '.php')) {
                    $body_view_path = 'mobiles/' . $body_view . '.php';
                } else {
                    show_error('Unable to load the requested file: ' . $tpl_name . '/' . $view_name . '.php');
                }
                $body = $this->ci->load->view($body_view_path, $data, TRUE);
                if (is_null($data)) {
                    $data = array('body' => $body);
                } else if (is_array($data)) {
                    $data['body'] = $body;
                } else if (is_object($data)) {
                    $data->body = $body;
                }
            }
            
            $this->ci->load->view('mobiles/templates/' . $tpl_view, $data);
        } else {
            if (!is_null($body_view)) {
                if (file_exists(APPPATH . 'views/' . $tpl_view . '/' . $body_view)) {
                    $body_view_path = $tpl_view . '/' . $body_view;
                } else if (file_exists(APPPATH . 'views/' . $tpl_view . '/' . $body_view . '.php')) {
                    $body_view_path = $tpl_view . '/' . $body_view . '.php';
                } else if (file_exists(APPPATH . 'views/' . $body_view)) {
                    $body_view_path = $body_view;
                } else if (file_exists(APPPATH . 'views/' . $body_view . '.php')) {
                    $body_view_path = $body_view . '.php';
                } else {
                    show_error('Unable to load the requested file: ' . $tpl_name . '/' . $view_name . '.php');
                }
                $body = $this->ci->load->view($body_view_path, $data, TRUE);
                if (is_null($data)) {
                    $data = array('body' => $body);
                } else if (is_array($data)) {
                    $data['body'] = $body;
                } else if (is_object($data)) {
                    $data->body = $body;
                }
            }
            $this->ci->load->view('templates/' . $tpl_view, $data);
        }
    }

}